package de.hs.rm.UJAdapter.event.impl.jmx;

import java.util.Hashtable;

import javax.management.AttributeChangeNotification;
import javax.management.Notification;
import javax.management.NotificationListener;

import org.osgi.service.upnp.UPnPDevice;
import org.osgi.service.upnp.UPnPService;

import de.hs.rm.UJAdapter.impl.UJDevice;
import de.hs.rm.UJAdapter.impl.UJService;

public class UJAttributeChangeNotificationListener implements NotificationListener {
	
	private UJDevice dev = null;
	private UPnPService service = null;
	private Hashtable listenerProps;

	@Override
	public void handleNotification(Notification notification,
            Object handback)
	{
		AttributeChangeNotification acn = (AttributeChangeNotification) notification;
		Hashtable events = new Hashtable(2);
		events.put(acn.getAttributeName(), acn.getNewValue());
		try {
			dev.generateEvent(listenerProps, events);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		System.out.println("\nReceived notification:");
//        System.out.println("\tClassName: " + notification.getClass().getName());
//        System.out.println("\tSource: " + notification.getSource());
//        System.out.println("\tType: " + notification.getType());
//        System.out.println("\tMessage: " + notification.getMessage());
//        if (notification instanceof AttributeChangeNotification) {
//            AttributeChangeNotification acn =
//                (AttributeChangeNotification) notification;
//            System.out.println("\tAttributeName: " + acn.getAttributeName());
//            System.out.println("\tAttributeType: " + acn.getAttributeType());
//            System.out.println("\tNewValue: " + acn.getNewValue());
//            System.out.println("\tOldValue: " + acn.getOldValue());
        
	}
	
	public void setListenerProps(UJDevice dev, UPnPService service) {
	    this.dev = dev;
	    this.service = service;
	    listenerProps = new Hashtable(6);
	    listenerProps.put(UPnPDevice.TYPE, dev.getDescriptions(null).get(UPnPDevice.TYPE));
	    listenerProps.put(UPnPDevice.UDN, dev.getDescriptions(null).get(UPnPDevice.UDN));
	    listenerProps.put(UPnPService.TYPE, service.getType());
	    listenerProps.put(UPnPService.ID, service.getId());
	  }

}
